﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using Utils.Common;
using ZK.BLL;
using ZK.BLL.Config;
using ZK.Entity;
using ZK.MyControl;

namespace ZK.Client.WinForm
{
    public partial class FrmPrintData : Form
    {
        public FrmPrintData()
        {
            InitializeComponent();
        }

        public static event Action<string> SelectedEvent;
 
        private void FrmPrintData_Load(object sender, EventArgs e)
        {
            //cmbprinter.DataSource = PrinterSettingConfig.GetConfig().PrinterSettingList;
            //cmbprinter.DisplayMember = nameof(PrinterSetting.PrinterName);
            //if (PrinterSettingConfig.GetConfig().PrinterSettingList.Count > 0)
            //{
            //    cmbprinter.SelectedIndex = 0;
            //}

            dxPager1.PagedEvent += DxPager1_PagedEvent;
            dxPager1.PageSize = SystemConfig.GetConfig().SystemInfoEntity.PageSize;
            AddColumns();
            LoadData();
 
        }
 
      
        private void DxPager1_PagedEvent(int pageIndex)
        {
            LoadData(pageIndex);
        }

        private void AddColumns()
        {

            dataView1.Columns.Add(DataGridColumnHelper.TextBoxColumnCreate(typeof(PrintData), nameof(PrintData.Id)));
            dataView1.Columns["Id"].Visible = false;
            //dataView1.Columns.Add(DataGridColumnHelper.TextBoxColumnCreate(typeof(PrintData), nameof(PrintData.PrinterName)));
            dataView1.Columns.Add(DataGridColumnHelper.TextBoxColumnCreate(typeof(PrintData), nameof(PrintData.CustomerName),true));
            dataView1.Columns.Add(DataGridColumnHelper.TextBoxColumnCreate(typeof(PrintData), nameof(PrintData.ProductName), true));
            dataView1.Columns.Add(DataGridColumnHelper.TextBoxColumnCreate(typeof(PrintData), nameof(PrintData.ModelNo)));
            dataView1.Columns.Add(DataGridColumnHelper.TextBoxColumnCreate(typeof(PrintData), nameof(PrintData.UserPrintName), true));
            dataView1.Columns.Add(DataGridColumnHelper.TextBoxColumnCreate(typeof(PrintData), nameof(PrintData.BatchNo), true));
            dataView1.Columns.Add(DataGridColumnHelper.TextBoxColumnCreate(typeof(PrintData), nameof(PrintData.Other), true));
            dataView1.Columns.Add(DataGridColumnHelper.TextBoxColumnCreate(typeof(PrintData), nameof(PrintData.PatentNO), true));
            dataView1.Columns.Add(DataGridColumnHelper.TextBoxColumnCreate(typeof(PrintData), nameof(PrintData.PersonName), true));
            dataView1.Columns.Add(DataGridColumnHelper.TextBoxColumnCreate(typeof(PrintData), nameof(PrintData.TemplateName), true));
            dataView1.Columns.Add(DataGridColumnHelper.TextBoxColumnCreate(typeof(PrintData), nameof(PrintData.PrintDate),"yyyy-MM-dd HH:mm:ss", true));
          
        }


        private void LoadData(int pageIndex = 1)
        {
            int pageSize = dxPager1.PageSize;

            string customer = txtCustomer.Text.Trim();
            string productName = txtProductName.Text.Trim();
            string modelNo = txtModelNo.Text.Trim();

            string batchNo = txtBatchNo.Text.Trim();
           
            FrmLoading.ShowWaiting(() => {
                DelegateAction.DoInvoke(this, () =>
                {


                    int total;
                    int pageCount;
                    dxPager1.PageIndex = pageIndex;
                    var list = PrintDataBLL.Instance.GetPageList(customer,productName, modelNo, batchNo,  pageIndex, pageSize, out total, out pageCount);

                    if (list.Count == 0 && pageIndex > 1)
                    {
                        dxPager1.PageIndex = --pageIndex;
                        list = PrintDataBLL.Instance.GetPageList(customer,productName, modelNo, batchNo, pageIndex, pageSize, out total, out pageCount);

                    }
                    dataView1.DataSource = list;
                    dxPager1.UpdatePageInfo(total, pageCount);
                });

            });
            
        }

        private void btnSearch_Click(object sender, EventArgs e)
        {
            LoadData();
        }

        private void btnDel_Click(object sender, EventArgs e)
        {

            if (MessageBox.Show("确认要删除选中数据?", "提示", MessageBoxButtons.YesNo, MessageBoxIcon.Warning) == DialogResult.Yes)
            {


                if (dataView1.CurrentCell!=null&& dataView1.CurrentCell.RowIndex>-1)
                {


                    string id = dataView1.Rows[dataView1.CurrentCell.RowIndex].Cells["Id"].Value.ToString();
                    PrintDataBLL.Instance.Delete(id);
                    LoadData(dxPager1.PageIndex);
                }
             
                
          
            }
        }

        private void dataView1_CellDoubleClick(object sender, DataGridViewCellEventArgs e)
        {
            if (e.RowIndex > -1)
            {
                if (MessageBox.Show("确定要加载选中数据到运行界面吗？", "提示", MessageBoxButtons.YesNo, MessageBoxIcon.Question) == DialogResult.Yes)
                {
                    string id = dataView1.Rows[e.RowIndex].Cells["Id"].Value.ToString();
                    if (SelectedEvent != null)
                    {
                        if (Class_Config.PrinetType == "WDJ" || Class_Config.PrinetType == "VDJ")
                        {

                            FormHelper.LoadForm(typeof(FrmMainExecute));
                        }
                        else
                        {

                            FormHelper.LoadForm(typeof(FrmCIJExecute));
                        }
                        SelectedEvent(id);
                    }
                }
            }
        }
    }
}
